Skip to content

fix(tokens): normalize inverted bbox in get_location instead of asserting - #676

Open
maxtaran2010 wants to merge 2 commits into
docling-project:mainfrom
maxtaran2010:fix/get-location-inverted-bbox
Open

fix(tokens): normalize inverted bbox in get_location instead of asserting#676
maxtaran2010 wants to merge 2 commits into
docling-project:mainfrom
maxtaran2010:fix/get-location-inverted-bbox

Conversation

@maxtaran2010

Copy link
Copy Markdown
Contributor

Fixes #620

Problem

`DocumentToken.get_location()` in `docling_core/types/doc/tokens.py` asserted `bbox[0] <= bbox[2]` and `bbox[1] <= bbox[3]`. Near-degenerate elements (e.g. a thin decorative rule whose layout-model regression produces a slightly inverted bbox) can violate this by a fraction of a point, which crashes `export_to_doctags(add_location=True)`:

File ".../docling_core/types/doc/tokens.py", line 307, in get_location
    assert bbox[1] <= bbox[3], f"bbox[1]<=bbox[3] => {bbox[1]}<={bbox[3]}"
AssertionError: bbox[1]<=bbox[3] => 633.3925132751465<=625.6789665222168

The asserts were redundant: the method already emits correctly-ordered location tokens via `min()`/`max()` downstream. The probability of hitting one such element grows with page count, so long documents are the most affected.

Fix

Sort the coordinates before normalization (mirroring the existing `min()`/`max()` behaviour) and drop the asserts. Output is unchanged for well-formed boxes and now correct instead of crashing for inverted ones.

Tests

Added `test/test_tokens.py` covering:

  • a normal bbox (exact token output)
  • the partially inverted bbox from the issue repro
  • a fully inverted bbox

All pass; existing `test_serialization_doctag.py` and `test_doctags_load.py` (17 tests) still pass, and `ruff` is clean.

…ting

Near-degenerate elements (e.g. a thin rule whose layout-model regression
produced a slightly inverted bbox) could arrive with bbox[0] > bbox[2] or
bbox[1] > bbox[3]. The redundant asserts in DocumentToken.get_location
crashed export_to_doctags(add_location=True) on such boxes, even though the
method already normalizes ordering via min()/max() downstream. This bites
long documents in particular, where the odds of one degenerate element grow
with page count.

Sort the coordinates before normalization to mirror the existing min()/max()
behaviour, and drop the asserts. Adds regression tests for normal, partially
inverted (from the issue repro), and fully inverted bboxes.

Fixes docling-project#620

Signed-off-by: max <87073104+maxtaran2010@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 8, 2026 08:03
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

DCO Check Passed

Thanks @maxtaran2010, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🔴 1 of 2 protections blocking · waiting on 👀 reviews

Protection Waiting on
🔴 Require two reviewer for test updates 👀 reviews
🟢 Enforce conventional commit

🔴 Require two reviewer for test updates

Waiting for

  • #approved-reviews-by >= 2
This rule is failing.

When test data is updated, we require two reviewers

  • #approved-reviews-by >= 2

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates DocumentToken.get_location() to handle slightly inverted bounding boxes by normalizing coordinate order instead of asserting, preventing crashes during DocTags export for near-degenerate layout elements (Issue #620).

Changes:

  • Normalize potentially inverted bbox coordinates using sorted() and remove the crash-causing assertions.
  • Add unit tests covering normal, partially inverted (issue repro), and fully inverted bounding boxes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
docling_core/types/doc/tokens.py Normalizes inverted bbox coordinates before computing location tokens, avoiding asserts/crashes.
test/test_tokens.py Adds regression/unit tests to ensure inverted bboxes serialize consistently and don’t crash.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docling_core/types/doc/tokens.py Outdated
@@ -303,13 +303,19 @@ def get_location(
self_closing: bool = False,
):
"""Get the location string give bbox and page-dim."""
Addresses review feedback on docling-project#676.

Signed-off-by: max <87073104+maxtaran2010@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redundant bbox assertion in DocumentToken.get_location crashes export_to_doctags on near-degenerate elements

2 participants